Page({
  data: {
    pageIndex: 0,
    PageSize: 20,
    key: "",
    noMore: false,
    customerList: [],
    currenti: null,
    currentj: null,
    startX: 0,
    currentId: null,
    options:["客户等级","装修进度","用户状态"],
    optionlist: [],
    optiontype: "",
    animationInfo: {}
  },
  onLoad() {
    this.getlist()
    var animation = dd.createAnimation({
      duration: 300,
      timeFunction: 'ease-in-out'
    });
    this.animation = animation;
    // animation.scale(0,0).step();
    this.setData({
      animationInfo: animation.export()
    })
  },
  getlist() {
    var _this = this
    if(!this.data.noMore){
      dd.showLoading({
        content: '加载中...',
      });
      getApp().req({
        url: "/customer/customerlist",
        data: {
          pageIndex: _this.data.pageIndex+1,
          PageSize: _this.data.PageSize,
          key: _this.data.key
        },
        cb(res){
          console.log(res)
          for (const i in res.data) {
            for (const j in res.data[i].CustomerDemands.Items) {
              res.data[i].CustomerDemands.Items[j].x = 0
              res.data[i].CustomerDemands.Items[j].y = 0
            }
          }
          if(res.data.length == _this.data.PageSize){

          }else{
            _this.setData({noMore: true})
          }
          if(_this.data.pageIndex == 0){
            var newlist = res.data
          }else{
            var newlist = _this.data.customerList.concat(res.data)
          }
          _this.setData({
            pageIndex:_this.data.pageIndex+1,
            customerList:newlist
            })
        },
        fail(){
          _this.setData({noMore: true})
          console.log(_this.data.noMore);
          
        },
        complete() {
          dd.hideLoading();
          dd.stopPullDownRefresh()
        }
      })
    }else{
      dd.showToast({
        content: '没有更多',
        duration: 1500
      });
    }
  },
  delItem(e) {
    console.log(e);
    var _this = this;
    dd.confirm({
      title: '确认提示',
      content: '确认删除此条记录吗？',
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      success: (result) => {
        if(result.confirm){
          getApp().req({
            url: "/customer/customerdemanddelete/"+ e.target.dataset.id,
            type: "POST",
            data: {Reason: ""},
            cb(res){
              _this.getlist()
            }
          })
        }
      },
    });
  },
  addmore() {
    console.log("low")
  },
  toEdit(e) {
    console.log(e);
  },
  optiontype(e) {
    console.log(e);
    var params = null
    var _this = this
    switch (+e.detail.value) {
      case 0: params = 44;
              this.setData({optiontype: 'lv'})
        break;
      case 1: params = 45;
              this.setData({optiontype: 'progress'})
        break;
      case 2: params = 46;
              this.setData({optiontype: 'state'})
        break;
      default:
        break;
    }
    getApp().req({
      url: "/customer/getlistdata/" + params,
      cb(res){
        console.log(res.data[0].ListItems.Items); 
        dd.getSystemInfo({
          success: (res) => {
            _this.animation.translateY(-res.windowHeight).step()
            _this.setData({
              animationInfo: _this.animation.export()
            })
            console.log(_this.data.animationInfo);
          }
        })
        _this.setData({
          optionlist:res.data[0].ListItems.Items,
          currentId:e.target.dataset.eid,
          // animationInfo: _this.animation.export()
          })
          console.log(_this.data.optionlist);
          console.log(_this.data.currentId);
          
      }
    })
  },
  optionchange(e) {
    console.log(e);
    var _this = this
    var url = "/customer";
    var data = {
      id: this.data.currentId
    }
    switch (this.data.optiontype) {
      case "lv": url += "/customerdemandchangelevel";
              data.levelId = e.target.dataset.id;
        break;
      case "progress": url += "/DecorationProgress";
              data.DecorationProgressId = e.target.dataset.id
        break;
      case "state": url += "/CustomerStatus";
              data.CustomerStatusId = e.target.dataset.id
        break;
      default:
        break;
    }
    
    getApp().req({
      url: url,
      type: "POST",
      data: data,
      cb(res){
        console.log(res);
        _this.getlist()
        
      }
    })
    this.closeOption()
  },
  closeOption() {
    this.animation.translateY(0).step({ duration: 10 })
    this.setData({
      animationInfo: this.animation.export()
    })
  },
  toDetail(e) {
    console.log(e);
    dd.navigateTo({
      url:"../crmcustomerdetail/crmcustomerdetail?id="+e.target.dataset.did
    })
  },
  moveStart(e) {
    // console.log("moveStart");
    // console.log(e);
    var list = this.data.customerList
    if(this.data.currenti == e.target.dataset.i && this.data.currentj == e.target.dataset.j){

    }else{
      // console.log("re");
      
      for (const i in list) {
        for (const j in list[i].CustomerDemands.Items) {
          list[i].CustomerDemands.Items[j].x = 0
          list[i].CustomerDemands.Items[j].y = 0
        }
      }
      list[e.target.dataset.i].CustomerDemands.Items[e.target.dataset.j].x = -1
      this.setData({
        customerList: list,
        currenti: e.target.dataset.i,
        currentj: e.target.dataset.j
        })
    }
    this.setData({
      startX:e.changedTouches[0].clientX
    })

  },
  moveend(e) {
    var list = this.data.customerList
    var x = e.changedTouches[0].clientX - this.data.startX
    var random = Math.random()*-1
    if(x<-50){
      list[e.target.dataset.i].CustomerDemands.Items[e.target.dataset.j].x = -100+random
    }else{
      list[e.target.dataset.i].CustomerDemands.Items[e.target.dataset.j].x = 0
    }
    this.setData({customerList:list})
    
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
    var _this = this
    this.setData({
      pageIndex:0,
      noMore:false
      })
    this.getlist()
  },
  onReachBottom() {
    // 页面被拉到底部
    this.getlist()
  },
});
